package com.umpay.util;

import com.umpay.dict.retcode.MerBusiEC;
import com.umpay.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName: UrlGainUtil  
 * @Description: 请求其他应用Url组装工具 
 * @author zhangtao  
 * @date Feb 26, 2018
 */
public class UrlGainUtil {
    
	private static final Logger log = LoggerFactory.getLogger(UrlGainUtil.class);

	public static String getServiceUrl(String hostKey,String serviceKey) {
		String url = null;
		PropertyUtil propertyUtil = (PropertyUtil) SpringUtil.getBean("propertyUtil");
		String hostUrl = StringUtil.trim(propertyUtil.getProperty(hostKey));
		String serviceUrl = StringUtil.trim(propertyUtil.getProperty(serviceKey));
		if(StringUtils.isEmpty(hostUrl)){
			log.error(String.format("配置参数[%s]未配置", hostKey));
			throw new BusinessException(MerBusiEC.SYSTEM_ERR, "系统忙");
		}
		if(StringUtils.isEmpty(serviceUrl)){
			log.error(String.format("配置参数[%s]未配置", serviceKey));
			throw new BusinessException(MerBusiEC.SYSTEM_ERR, "系统忙");
		}
		if(hostUrl.endsWith("/")) { //host地址以"/结尾"
			if(serviceUrl.startsWith("/")) {
				url = hostUrl + serviceUrl.substring(1);
			}else {
				url = hostUrl + serviceUrl;
			}
		}else {
			if(serviceUrl.startsWith("/")) {
				url = hostUrl + serviceUrl;			
			}else {
				url = hostUrl + "/" + serviceUrl;				
			}
		}
		return url;
	}
}
